package com.sc.web.statistics;

import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sc.dal.mapper.member.AgentMapper;
import com.sc.dal.mapper.member.MemberMapper;
import com.sc.dal.mapper.order.OrderMapper;
import com.sc.dal.model.order.Orders;
import com.sc.service.goods.GoodsService;
import com.sc.util.data.DateUtil;
import com.sc.util.exception.ScBizException;


@RequestMapping(value="/welcome")
@Controller
public class WelcomeController {
	
	@Autowired
	private GoodsService GoodsService;
	/**
	 * 代理商
	 */
	@Autowired
	private AgentMapper agentMapper;
	//会员统计
	@Autowired
	private MemberMapper memberMapper;
	//订单统计
	@Autowired
	private OrderMapper orderMapper;
	
	@RequestMapping(value="/initWelcome")
	@ResponseBody
	public Object initWelcome(HttpServletRequest request){
		Map<String, Object> map = new HashMap<String, Object>();
		//  1：总数  2：种类数
		List<Integer> goodsList = GoodsService.findTotal();
		//获取当月第一天
		String firstMonth = DateUtil.getFirstMonth();
		//获取当月最后一天
		String lastMonth = DateUtil.getLastMonth();
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("firstMonth", firstMonth);
		param.put("lastMonth", lastMonth);
		List<Integer> agentList = agentMapper.findTotal(param);
		
		//统计会员
		String week = DateUtil.getWeekTime();
		Map<String, Object> paramMember = new HashMap<String, Object>();
		paramMember.put("week", week);
		paramMember.put("firstMonth", firstMonth);
		List<Integer> memberList = memberMapper.queryTotal(paramMember);
		//统计订单销售金额
		String today = DateUtil.formatYMDToString(new Date()); //获取今日
		String yesterday = DateUtil.formatYMDToString(DateUtil.getDateBefore(new Date(), 1)); //获取昨日
		Map<String, Object> paramOrders = new HashMap<String, Object>();
		paramOrders.put("today", today);
		paramOrders.put("yesterday", yesterday);
		paramOrders.put("firstMonth", firstMonth);
//		Map<String, Object> object = orderMapper.queryTotal(paramOrders);
		List<String> orderList = null;
		try {
			orderList = orderMapper.querySumTotal(paramOrders);
		} catch (Exception e) {
			throw new ScBizException("查询异常！", e);
		}
		
		map.put("goodsList", goodsList);
		map.put("agentList", agentList);
		map.put("memberList", memberList);
		map.put("orderList", orderList);
		return map;
	}
	
	

}
